﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Common
{
    public class XML
    {
        public static string _XMLPath = System.Windows.Forms.Application.ExecutablePath + ".config";

        public static List<Cfg_Row> Convert2Row(string sectionName)
        {
            try
            {
                List<Cfg_Row> rowList = new List<Cfg_Row>();
                XmlDocument doc = new XmlDocument();
                doc.Load(_XMLPath);
                XmlNodeList list = doc.SelectNodes("//" + sectionName);
                XmlNode rootnode = list[0];

                foreach (XmlNode _node in rootnode.ChildNodes)
                {
                    Cfg_Row row = new Cfg_Row();
                    row.FieldList = new List<Cfg_Field>();
                    foreach (XmlNode child_node in _node.ChildNodes)
                    {
                        Cfg_Field field = new Cfg_Field();
                        field.name = child_node.Name;
                        field.value = child_node.InnerText;
                        row.FieldList.Add(field);
                    }

                    rowList.Add(row);
                }

                return rowList;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void Add(string sectionName, Cfg_Row cfg_row)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_XMLPath);
                XmlElement rootnode = (XmlElement)doc.SelectSingleNode("//" + sectionName);
                XmlNode newItemNode = doc.CreateNode("element", rootnode.FirstChild.Name, "");
                rootnode.AppendChild(newItemNode);
                for (var i = 0; i < cfg_row.FieldList.Count; i++)
                {
                    Cfg_Field cfg_Field = cfg_row.FieldList[i];
                    XmlNode newNode = doc.CreateNode("element", cfg_Field.name, "");
                    newNode.InnerText = cfg_Field.value;
                    newItemNode.AppendChild(newNode);
                }

                doc.Save(_XMLPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static XmlNode GetRow(string sectionName, List<Cfg_Field> primaryFieldList, XmlDocument doc)
        {
            try
            {
                XmlNodeList list = doc.SelectNodes("//" + sectionName);
                XmlNode rootnode = list[0];
                foreach (XmlNode _node in rootnode.ChildNodes)
                {
                    bool isfind = false;
                    foreach (var primaryField in primaryFieldList)
                    {
                        isfind = false;
                        foreach (XmlNode child_node in _node.ChildNodes)
                        {
                            if (child_node.Name.Equals(primaryField.name) && child_node.InnerText.Equals(primaryField.value))
                            {
                                isfind = true;
                                break;
                            }
                        }

                        //没找到直接跳出
                        if (!isfind)
                        {
                            break;
                        }
                    }

                    if (isfind)
                    {
                        return _node;
                    }
                }

                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void Update(string sectionName, string primaryName, Cfg_Row cfg_Row)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_XMLPath);
                List<Cfg_Field> primaryFieldList = new List<Cfg_Field>();
                string[] primaryFieldArr = primaryName.Split('#');
                foreach (var s in primaryFieldArr)
                {
                    Cfg_Field primaryField = cfg_Row.FieldList.Where(p => p.name.Equals(s)).FirstOrDefault();
                    primaryFieldList.Add(primaryField);
                }

                XmlNode xmlNode = GetRow(sectionName, primaryFieldList, doc);
                if (xmlNode != null)
                {
                    foreach (XmlNode child_node in xmlNode.ChildNodes)
                    {
                        Cfg_Field field = cfg_Row.FieldList.Where(p => p.name.Equals(child_node.Name)).FirstOrDefault();
                        if (field != null)
                        {
                            child_node.InnerText = field.value;
                        }
                    }
                }

                doc.Save(_XMLPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void Delete(string sectionName, Cfg_Field primaryField)
        {
            Delete(sectionName, new List<Cfg_Field>() { primaryField });
        }

        public static void Delete(string sectionName, List<Cfg_Field> primaryFieldList)
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(_XMLPath);

                XmlNode xmlNode = GetRow(sectionName, primaryFieldList, doc);
                if (xmlNode != null)
                {
                    XmlNodeList list = doc.SelectNodes("//" + sectionName);
                    XmlNode rootnode = list[0];
                    if (rootnode != null)
                    {
                        rootnode.RemoveChild(xmlNode);
                    }
                }

                doc.Save(_XMLPath);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
